﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using CDB.LinqModel.Data;
using CDB.LinqData;
using CDB.LinqModel;

public partial class back_AddTestimonials : System.Web.UI.Page
{
    #region Variable Declaration
    IDataRepository _dataObject = new DataRepository();
    Testimonial testimonial;
    #endregion

    protected void Page_Load(object sender, EventArgs e)
    {
        if (!Page.IsPostBack)
        {
            if (!string.IsNullOrEmpty(Request.QueryString["id"]))
            {
                testimonial = new Testimonial(_dataObject);
                testimonial.Id = Convert.ToInt32(Request.QueryString["id"]);
                testimonial.GetTestimonialById();

                if (!string.IsNullOrEmpty(testimonial.By))
                {
                    txtAddedBy.Text = testimonial.By;
                    txtTestimonial.InnerHtml = testimonial.Content;
                }
            }
        }
    }
    protected void btnSubmit_Click(object sender, EventArgs e)
    {
        testimonial = new Testimonial(_dataObject);
        testimonial.By = txtAddedBy.Text;
        testimonial.Content = txtTestimonial.InnerHtml;

        if (checkIfNewsExists(testimonial.By))
        {
            divMessage.InnerHtml = "<strong>On snap!</strong>&nbsp;Testimonial with the same name Exists!";
            divMessage.Attributes.Add("class", "alert alert-error");
        }
        else
        {
            if (string.IsNullOrEmpty(Request.QueryString["id"]))
            {
                testimonial.Create();
                divMessage.InnerHtml = "<strong>Well done!</strong>&nbsp;Testimonial inserted successfully!";
                divMessage.Attributes.Add("class", "alert alert-success");
            }
            else
            {
                testimonial.Id = Convert.ToInt32(Request.QueryString["id"]);
                testimonial.Update();
                divMessage.InnerHtml = "<strong>Well done!</strong>&nbsp;Testimonial updated successfully!";
                divMessage.Attributes.Add("class", "alert alert-success");
            }
            clearFields();
        }

    }
    private bool checkIfNewsExists(string addedBy)
    {
        Testimonial testimonial = new Testimonial(_dataObject);
        testimonial.By = addedBy;
        testimonial.GetTestimonialByCreatedBy();

        if (testimonial.Id != 0)
            return true;
        else
            return false;
    }
    private void clearFields()
    {
        txtAddedBy.Text = string.Empty;
        txtTestimonial.InnerText = string.Empty;
    }
}